<?php


namespace App\Console\Commands\Marketing;


use App\Models\Marketing\ShopAdvertisementModel;
use Illuminate\Console\Command;

class ChangeAdvertisementStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'marketing:change-advertisement-status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '闪屏广告过期自动禁用';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle()
    {
        ShopAdvertisementModel::query()
                              ->where('status', ShopAdvertisementModel::STATUS_ENABLE)
                              ->where('endAt', '<', time())
                              ->chunk(500, function ($advList) {
                                  $ids = collect($advList)->pluck('id')->toArray();
                                  ShopAdvertisementModel::query()
                                                        ->whereIn('id', $ids)
                                                        ->update([
                                                            'status'    => ShopAdvertisementModel::STATUS_DISABLE,
                                                            'updatedAt' => time()
                                                        ]);
                                  $this->info('变更状态数目:' . count($ids));
                              });
    }

}